//
//  HomeViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/25.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "HomeViewController.h"

#import "SearchViewController.h"
#import "CollectionViewController.h"
#import "CommonViewController.h"
#import "PushMessageInfoViewController.h"
#import "ContactViewController.h"
#import "HomeWhitePage.h"
#import "HomeMge.h"
#import "HomeScrollView.h"
#import "Common.h"
#import "CommonView.h"
#import "iCarousel.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"

#import "SVProgressHUD.h"
#import "BGMAcceptView.h"
#import "BGMShareClass.h"

#import "HomePublicView.h"

#import "BGMShareView.h"


#define puclicSize 10


@interface HomeViewController ()
<
iCarouselDelegate,iCarouselDataSource,
HomeMgeDelegate,BGMAcceptViewDelegate,
HomeWhitePageDelgate,HomePublicViewDelegate,
BGMShareViewDelegate
>

//@property (nonatomic, strong)HomeScrollView *homeScrollView;

@property (nonatomic, strong)iCarousel *userCarousel;
@property (nonatomic, strong)iCarousel *publicCarousel;

@property (nonatomic, strong)HomeMge *homeView;
@property (nonatomic, strong)HomeWhitePage *whitePage;
@property (nonatomic, strong)HomePublicView *publicPage;

@property (nonatomic, strong)NSString *currentType;//1公益 0好友

@property (nonatomic, strong)UIScrollView *homeSC;

//@property (nonatomic, strong)UIView *selectView;
@property (nonatomic, strong)NSMutableArray *userList;  //好友

@property (nonatomic, strong)NSMutableArray *publicList; //公益
@property (nonatomic, assign)int currentPublicPage;
@property (nonatomic, assign)BOOL publicHaveData;

@property (nonatomic, strong)BGMAcceptView *acceptView;
@property (nonatomic, strong)NSDictionary *currentUserDic;  //当前好友发布信息
@property (nonatomic, strong)NSDictionary *currentPublicDic;//当前公益发布信息

@property (nonatomic, assign)int currentIndex;  //user 关注受理
@property (nonatomic, assign)int currentPuclicIndex; //public


@property (nonatomic, strong)NSMutableArray *changeData;//user
@property (nonatomic, strong)NSMutableArray *publicChangeData;//public


@property (nonatomic, strong)UIButton *friendBtn;
@property (nonatomic, strong)UIButton *welfareBtn;
@property (nonatomic, strong)UILabel *lineLabel;

@property (nonatomic, strong)BGMShareView *shareView;


@property (nonatomic, assign)BOOL isSearch;
@property (nonatomic, strong)NSDictionary *searchDic;
@property (nonatomic, assign)int searchPage;


@end

@implementation HomeViewController

- (void)userOpenAddressRequestHome{

//    if ([self.currentType isEqualToString:@"0"]) {
//        if (self.userList.count<=0) {
//            [self requestMessage];
//        }
//    }else{
//
//    }
    
   
}
- (void)viewWillAppear:(BOOL)animated{
//    if ([self.currentType isEqualToString:@"0"]) {
//        [self.userList removeAllObjects];
//        [SVProgressHUD show];
//        [self requestMessage];
//    }else{
//
//    }
}


- (void)UserSearchMsg:(NSNotification *)noti{
    /*
    self.isSearch = YES;
    NSDictionary *dic = noti.object;
    NSArray *array = [dic objectForKey:@"array"];
    self.searchDic = [dic objectForKey:@"param"];
    self.searchPage = [[NSString stringWithFormat:@"%@",[self.searchDic objectForKey:@"pageNum"]]intValue];
    
    if ([self.currentType isEqualToString:@"0"]) {
        //好友
//        [self.userList removeAllObjects];
//        [self.changeData removeAllObjects];
//        self.currentUserDic = nil;
//        [self reloadScrollerView:array];  //不分页
    }else{
        //公益
//        [self.publicList removeAllObjects];
//        [self.publicChangeData removeAllObjects];
//
//        self.currentPublicPage = 1;
//        self.publicHaveData = YES;
//        self.currentPublicDic = nil;
//
//        self.currentPuclicIndex = 0;
//        [self.publicChangeData removeAllObjects];
//        [self.publicCarousel reloadData];
//
//        if (array.count==10) {
//            self.publicHaveData = YES;
//        }
//        [self reloadPublicView:array];
//        [self.publicCarousel reloadData];

    }
     )
     */
}

//关注列表 关注列表 删除
- (void)changeInfo:(NSNotification *)noti{
    NSArray *arry = noti.object;
    for (NSDictionary *followDic in arry) {
        NSString *type = [NSString stringWithFormat:@"%@",[followDic objectForKey:@"type"]];
        if ([type isEqualToString:@"0"]) {  //好友
            
            for(int i = 0;i<self.changeData.count;i++) {
                NSMutableDictionary *dic = [self.changeData objectAtIndex:i];
                [dic setValue:@"NO" forKey:@"itisFollow"];
                [self.changeData replaceObjectAtIndex:i withObject:dic];
            }
            
            NSArray *array = [followDic objectForKey:@"dic"];
            for (NSString *pid in array) {
                for (int i = 0;i<self.changeData.count;i++) {
                    NSMutableDictionary *homePubDic = [self.changeData objectAtIndex:i];
                    NSString *userPid = [homePubDic objectForKey:@"pid"];
                    if ([pid  isEqualToString:userPid]) {
                        [homePubDic setValue:@"YES" forKey:@"itisFollow"];
                        [self.changeData replaceObjectAtIndex:i withObject:homePubDic];
                    }
                    
                    
                }
                
            }
            
        }else if([type isEqualToString:@"1"]){ //公益
            for(int i = 0;i<self.publicChangeData.count;i++) {
                NSMutableDictionary *dic = [self.publicChangeData objectAtIndex:i];
                [dic setValue:@"NO" forKey:@"itisFollow"];
                [self.publicChangeData replaceObjectAtIndex:i withObject:dic];
            }
            
            NSArray *array = [followDic objectForKey:@"dic"];
            for (NSString *pid in array) {
                for (int i = 0;i<self.publicChangeData.count;i++) {
                    NSMutableDictionary *homePubDic = [self.publicChangeData objectAtIndex:i];
                    NSString *userPid = [homePubDic objectForKey:@"pid"];
                    if ([pid  isEqualToString:userPid]) {
                        [homePubDic setValue:@"YES" forKey:@"itisFollow"];
                        [self.publicChangeData replaceObjectAtIndex:i withObject:homePubDic];
                    }

                }
                
            }
            
            
        }
  
    }
    
    
    
    if ([self.currentType isEqualToString:@"0"]) { //素友帮
        NSDictionary *otherDic = [self.changeData objectAtIndex:self.currentIndex];
        [self.homeView setMessageWithView:self.currentUserDic changeData:otherDic];
        
    }else{
        NSDictionary *otherDic = [self.publicChangeData objectAtIndex:self.currentPuclicIndex];
        [self.publicPage setPublicMsg:self.currentPublicDic isFollowDic:otherDic];
    }

}
- (void)changeInfo_info:(NSNotification *)noti{
    NSDictionary *dic = noti.object;
    if([dic objectForKey:@"acceptance"]){
        NSString *accid = [dic objectForKey:@"acceptance"];
        for(int i = 0;i<self.changeData.count;i++) {
            NSMutableDictionary *dic = [self.changeData objectAtIndex:i];
            NSString *str = [dic objectForKey:@"pid"];
            if ([accid isEqualToString:str]) {
                [dic setValue:@"YES" forKey:@"itisAppent"];
                [self.changeData replaceObjectAtIndex:i withObject:dic];
            }
        }
        
        NSDictionary *otherDic = [self.changeData objectAtIndex:self.currentIndex];
        [self.homeView setMessageWithView:self.currentUserDic changeData:otherDic];
    }
    else if([dic objectForKey:@"type"]){
        NSString *type = [dic objectForKey:@"type"];
        NSString *pid = [dic objectForKey:@"pid"];
        NSString *follow = [dic objectForKey:@"itisFollow"];

        if ([type isEqualToString:@"0"]) {  //好友
            
            for(int i = 0;i<self.changeData.count;i++) {
                NSMutableDictionary *newDic = [self.changeData objectAtIndex:i];
                NSString *pids = [newDic objectForKey:@"pid"];
                if ([pid isEqualToString:pids]) {
                    if ([follow isEqualToString:@"YES"]) {
                        [newDic setValue:@"YES" forKey:@"itisFollow"];
                    }else{
                        [newDic setValue:@"NO" forKey:@"itisFollow"];
                    }
                    [self.changeData replaceObjectAtIndex:i withObject:newDic];
                    break;
                }
                
            }
           
            
        }
        else{  //公益
            for(int i = 0;i<self.publicChangeData.count;i++) {
                NSMutableDictionary *newDic = [self.publicChangeData objectAtIndex:i];
                NSString *pids = [newDic objectForKey:@"pid"];
                if ([pid isEqualToString:pids]) {
                    if ([follow isEqualToString:@"YES"]) {
                          [newDic setValue:@"YES" forKey:@"itisFollow"];
                    }else{
                          [newDic setValue:@"NO" forKey:@"itisFollow"];
                    }
                    [self.publicChangeData replaceObjectAtIndex:i withObject:newDic];
                    break;
                }
              
            }
        }
        
        
        if ([self.currentType isEqualToString:@"0"]) { //素友帮
            NSDictionary *otherDic = [self.changeData objectAtIndex:self.currentIndex];
            [self.homeView setMessageWithView:self.currentUserDic changeData:otherDic];
            
        }else{
            NSDictionary *otherDic = [self.publicChangeData objectAtIndex:self.currentPuclicIndex];
            [self.publicPage setPublicMsg:self.currentPublicDic isFollowDic:otherDic];
        }
        
        
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentType = @"0";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userOpenAddressRequestHome) name:@"getHomeMessage" object:nil];
  
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(freshHomeMsg) name:@"freshHomeMsg" object:nil];
    
    //转发好友成功
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fowardPusheMsgInfo:) name:@"fowardPusheMsgInfoHome" object:nil];
    
    //关注 收藏下一级修改
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeInfo:) name:@"HomeChangeInfo" object:nil];
    
    // 详情修改
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeInfo_info:) name:@"HomeChangeInfo_info" object:nil];
    
    
    // 搜索
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UserSearchMsg:) name:@"UserSearchMsg" object:nil];
    
    
    
    self.topBackView.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [SVProgressHUD show];
    self.userList = [[NSMutableArray alloc]init];
    self.changeData = [[NSMutableArray alloc]init];
    
    self.publicList =  [[NSMutableArray alloc]init];
    self.publicChangeData = [[NSMutableArray alloc]init];

    self.currentIndex = 0;
    self.currentPuclicIndex = 0;
    
    
    self.currentPublicPage = 1;
    self.publicHaveData = YES;  //是否还有下一步
    
//    UIButton *searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 40, 25, 25)];
//    [searchBtn setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
//    searchBtn.tag = 99;
//    [self.view addSubview:searchBtn];
    
    UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 40, 100, 30)];
    topLabel.text = @"素友";
    topLabel.textColor = bgmmainColor;
    topLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:topLabel];
    
    
    
    UIButton *collection = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-55, 40, 25, 25)];
    [collection setBackgroundImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
    collection.tag = 100;
    [self.view addSubview:collection];
    
//    [searchBtn addTarget:self action:@selector(gotoNextVC:) forControlEvents:UIControlEventTouchUpInside];
    
    [collection addTarget:self action:@selector(gotoNextVC:) forControlEvents:UIControlEventTouchUpInside];

 
    float w = 100;
    self.friendBtn = [[UIButton alloc]initWithFrame:CGRectMake((SCREENW-w*2)/2, 80, w, 40)];
    [self.friendBtn setTitle:@"素友帮" forState:UIControlStateNormal];
    self.friendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.friendBtn setTitleColor:bgmColor(54, 54, 54) forState:UIControlStateNormal];
    [self.friendBtn setTitleColor:bgmmainColor forState:UIControlStateSelected];
    [self.view addSubview:self.friendBtn];
    self.friendBtn.tag = 99;
    self.friendBtn.selected = YES;
    
    
    self.welfareBtn = [[UIButton alloc]initWithFrame:CGRectMake((SCREENW-w*2)/2+w, 80, w, 40)];
    [self.welfareBtn setTitle:@"素公益" forState:UIControlStateNormal];
    self.welfareBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.welfareBtn setTitleColor:bgmColor(54, 54, 54) forState:UIControlStateNormal];
    [self.welfareBtn setTitleColor:bgmmainColor forState:UIControlStateSelected];
    self.welfareBtn.tag = 100;
    [self.view addSubview:self.welfareBtn];
    
    [self.friendBtn addTarget:self action:@selector(clickHomeTopBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.welfareBtn addTarget:self action:@selector(clickHomeTopBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.lineLabel =[[UILabel alloc]initWithFrame:CGRectMake(self.friendBtn.frame.origin.x+25, self.friendBtn.frame.origin.y+40,50 ,2)];
    self.lineLabel.backgroundColor = bgmmainColor;
    [self.view addSubview:self.lineLabel];

    
 
 
    
    self.homeSC = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.lineLabel.frame.origin.y+30, SCREENW, SCREENH-((SCREENH>736?90:50))-(self.lineLabel.frame.origin.y+30))];
    self.homeSC.contentSize = CGSizeMake(SCREENW*2, SCREENH-((SCREENH>736?90:50))-(self.lineLabel.frame.origin.y+30));
    self.homeSC.pagingEnabled = YES;
    self.homeSC.backgroundColor = [UIColor clearColor];
    self.homeSC.showsHorizontalScrollIndicator = NO;
    self.homeSC.scrollEnabled = NO;
    [self.view addSubview:self.homeSC];
    
    float h = SCREENH-(SCREENH>736?90:50)-(self.lineLabel.frame.origin.y+30);
    h= h/5*4;
    
    self.homeView = [[HomeMge alloc]initWithFrame:CGRectMake(25, 0, SCREENW-50, h)];
    self.homeView.homeDelegate = self;
    [self.homeSC addSubview:self.homeView];
    self.homeView.hidden = YES;
    
    self.whitePage = [[HomeWhitePage alloc]initWithFrame:self.homeView.frame];
    self.whitePage.homeDelegate = self;
    [self.homeSC addSubview:self.whitePage];
    
    
    
    
    self.publicPage = [[HomePublicView alloc]initWithFrame:CGRectMake(SCREENW+25, 0, SCREENW-50, h)];
    self.publicPage.publicPageDelegate = self;
    [self.homeSC addSubview:self.publicPage];


    
    self.userCarousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, self.homeView.frame.size.height-15, SCREENW, 100)];
    self.userCarousel.delegate = self;
    self.userCarousel.dataSource = self;
    self.userCarousel.bounces = NO;
    self.userCarousel.pagingEnabled = YES;
    self.userCarousel.type = iCarouselTypeCustom;
    self.userCarousel.tag = 100;
    [self.homeSC addSubview:self.userCarousel];
   
    
    
    self.publicCarousel = [[iCarousel alloc]initWithFrame:CGRectMake(SCREENW, self.userCarousel.frame.origin.y, SCREENW, 100)];
    self.publicCarousel.delegate = self;
    self.publicCarousel.dataSource = self;
    self.publicCarousel.bounces = NO;
    self.publicCarousel.pagingEnabled = YES;
    self.publicCarousel.type = iCarouselTypeCustom;
    self.publicCarousel.tag = 200;
    [self.homeSC addSubview:self.publicCarousel];
    
    
    
 
    UIApplication *app = [UIApplication sharedApplication];
    self.acceptView = [[BGMAcceptView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    self.acceptView.acceptDelegate = self;
    [app.keyWindow addSubview:self.acceptView];
    self.acceptView.hidden = YES;
    NSString *isOpenContactStore = [BGMUser shareInstance].isOpenContactStore;
    if([isOpenContactStore isEqualToString:@"1"]){
        [self requestMessage];
    }else{
        self.whitePage.hidden = NO;
        [self.whitePage homeNotOpenPage];
    }
    
    [self requestPublicMessage];
    
    self.shareView = [[BGMShareView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    self.shareView.shareDelegate = self;
    [app.keyWindow addSubview:self.shareView];
    self.shareView.hidden = YES;
  
}
#pragma mark 切换好友/公益
-(void)clickHomeTopBtn:(UIButton *)btn{
    int tag = (int)btn.tag;
    if (tag == 99) {  //素友帮
        self.currentType = @"0";
        self.friendBtn.selected = YES;
        self.welfareBtn.selected = NO;
       
        self.friendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        self.welfareBtn.titleLabel.font = [UIFont systemFontOfSize:18];

        [UIView animateWithDuration:0.3 animations:^{
            self.lineLabel.frame = CGRectMake(self.friendBtn.frame.origin.x+25, self.friendBtn.frame.origin.y+40,50 ,2);
            self.homeSC.contentOffset = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            self.userCarousel.hidden = NO;
            self.publicCarousel.hidden = YES;
            
        }];
        
        
    }else{  //素公益
        self.currentType = @"1";
    
        self.friendBtn.selected = NO;
        self.welfareBtn.selected = YES;
        self.welfareBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        self.friendBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.lineLabel.frame = CGRectMake(self.welfareBtn.frame.origin.x+25, self.friendBtn.frame.origin.y+40,50 ,2);
            self.homeSC.contentOffset = CGPointMake(SCREENW, 0);
        } completion:^(BOOL finished) {
            self.userCarousel.hidden = YES;
            self.publicCarousel.hidden = NO;
            
        }];
        
        
    }
}

#pragma mark 搜索&&关注页面
- (void)gotoNextVC:(UIButton *)btn{
    int tag = (int)btn.tag;
    if (tag == 99) {
        SearchViewController *vc = [[SearchViewController alloc]init];
        vc.isPublic = self.currentType;  //0 素友 1公益
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tag == 100){
        CollectionViewController *vc = [[CollectionViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark 转发评论(好友)
-(void)clickCommon{
    CommonViewController *vc = [[CommonViewController alloc]init];
    vc.publishId = [NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"publishId"]];
    vc.publishHelpUserId = [NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"helpUsersId"]];
    vc.userId = [NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"userId"]];;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 转发(公益)
- (void)clickforward{
    
    
}




#pragma mark 关注事件(好友)
- (void)clickHomeBtn:(UIButton *)btn dic:(NSDictionary *)dic{
    NSMutableDictionary *chagenDic = [self.changeData objectAtIndex:self.currentIndex];
    if (btn.selected) {
        [chagenDic setValue:@"YES" forKey:@"itisFollow"];
        [self requestAddFollow:dic];
    }else{
        [chagenDic setValue:@"NO" forKey:@"itisFollow"];
        [self requestCancelFollow:dic];
    }
}
#pragma mark 取消关注事件(好友)
- (void)requestCancelFollow:(NSDictionary *)dic{
    if ([dic objectForKey:@"pid"]) {
        NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"publishId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"pid"]],@"usersId":[BGMUser shareInstance].userId};
        
        [BGMHTTP requestWithMethodName:@"index/removeFollow" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
            NSLog(@"asdfsaff");
        }];

    }else{
        NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"publishId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]],@"usersId":[BGMUser shareInstance].userId};
        
        [BGMHTTP requestWithMethodName:@"index/removeFollow" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
            NSLog(@"asdfsaff");
        }];

    }
    
    
    
    
}
#pragma mark 关注事件(好友)
- (void)requestAddFollow:(NSDictionary *)dic{
    
    if ([dic objectForKey:@"pid"]) {
        
        NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"publishId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"pid"]]};
        [BGMHTTP requestWithMethodName:@"index/addFollow" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
            
        }];
        
    }else{
        NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"publishId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]]};
        
        [BGMHTTP requestWithMethodName:@"index/addFollow" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
            
        }];
        
    }
    
    
}

#pragma mark 受理事件(好友)
- (void)clickAccept{
    
    NSMutableDictionary *chagenDic = [self.changeData objectAtIndex:self.currentIndex];
    NSString *itisAppent = [chagenDic objectForKey:@"itisAppent"];
    if ([itisAppent isEqualToString:@"YES"]) {
        [SVProgressHUD showInfoWithStatus:@"已经受理过啦！"];
    }else{
         self.acceptView.hidden = NO;
    }
}
- (void)sendAccept:(NSString *)str{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showWithStatus:@""];
    if (str.length == 0) {
        str = @"";
        [SVProgressHUD showInfoWithStatus:@"受理留言不能为空！"];
        return;
    }
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,
                             @"acceptanceId":[BGMUser shareInstance].userId,
                             @"publishId":[NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"publishId"]],
                             @"userId":[NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"userId"]],
                             @"acceptanceMessage":str,
                             @"helpId":[NSString stringWithFormat:@"%@",[self.currentUserDic objectForKey:@"helpUsersId"]]
                             };
    [BGMHTTP requestWithMethodName:@"index/publishAcceptance" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hiddenAcceptView];
            NSMutableDictionary *chagenDic = [self.changeData objectAtIndex:self.currentIndex];
            [chagenDic setValue:@"YES" forKey:@"itisAppent"];
            self.homeView.rightBtn.selected = YES;
        });
    }];
    
}
- (void)hiddenAcceptView{
    self.acceptView.hidden = YES;
    [self.acceptView.textView resignFirstResponder];
    self.acceptView.textView.text = @"";
}
//转发到好友聊天
- (void)fowardPusheMsgInfo:(NSNotification *)noti{
    NSDictionary *dic = noti.object;
    BGMShareClass *shareClass = [[BGMShareClass alloc]init];
    [shareClass forwardPublishType:@"2" publishId:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pid"]] pusblishUserId:[NSString stringWithFormat:@"%@",[dic objectForKey:@"usersId"]] mobShareTitle:@""];
}
#pragma mark 分享(公益)
-(void)clickPublicShare{
    [self.shareView msgType:YES];
    self.shareView.hidden = NO;
}
#pragma mark 关注(公益)
-(void)clickPublicFollow:(UIButton *)btn{
    
    NSMutableDictionary *chagenDic = [self.publicChangeData objectAtIndex:self.currentPuclicIndex];
    if (btn.selected) {
        [chagenDic setValue:@"YES" forKey:@"itisFollow"];
        [self requestAddFollow:self.currentPublicDic];
    }else{
        [chagenDic setValue:@"NO" forKey:@"itisFollow"];
        [self requestCancelFollow:self.currentPublicDic];
    }
}
- (void)clickCancel{
    self.shareView.hidden = YES;
}
- (void)clickShare:(int)shareType{
    self.shareView.hidden = YES;
    switch (shareType) {
        case 101:
        {
            ContactViewController *vc = [[ContactViewController alloc]init];
            vc.messageDic = self.currentPublicDic;
            vc.isForward = YES;
            vc.isForwardFormHome = YES;
            vc.messageId = [NSString stringWithFormat:@"%@",[self.currentPublicDic objectForKey:@"pid"]];
            vc.helpUsersId = @"";
            vc.connectionUserId = @"";
            UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
            [self.navigationController presentViewController:nc animated:YES completion:nil];
        }
            break;
        case 102:
        {
            
            NSString *title = [self.currentPublicDic objectForKey:@"ptitle"];
            BGMShareClass *shareClass = [[BGMShareClass alloc]init];
//            [shareClass sharetype:102 publishId: [NSString stringWithFormat:@"%@",[self.currentPublicDic objectForKey:@"pid"]] pusblishUserId: [NSString stringWithFormat:@"%@",[self.currentPublicDic objectForKey:@"usersId"]] mobShareTitle:title];
            [shareClass sharetype:102 publishId:[NSString stringWithFormat:@"%@",[self.currentPublicDic objectForKey:@"pid"]] pusblishUserId:[BGMUser shareInstance].userId mobShareTitle:title];
            
        }
            break;
        default:
            break;
    }

}

#pragma mark 进入发布详情页面(好友)
- (void)gotoMessageInfo:(NSDictionary *)dic{
    PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
    vc.messageId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]];
    NSString *helpUsersId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"helpUsersId"]];
    
    vc.helpUsersId = helpUsersId.length>0?helpUsersId:@"";
    vc.connectionUserId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"connectionUserId"]];
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark 进入发布详情页面(公益)
- (void)clickPublicMsg{
    PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
    vc.messageId = [NSString stringWithFormat:@"%@",[self.currentPublicDic objectForKey:@"pid"]];
    vc.isPublicMsg = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 未开始通讯录DELEGATE
- (void)clickOpenAddressBook{
   UIAlertController *alter = [CommonView getCommonAlert:@"" message:@"去手机设置->隐私->通讯录，允许素友通讯录访问吧~" ok:@"确定" cancel:@"取消" completed:^(NSInteger index) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [self gotoIphoneSet:index];
       });
    }];
    
    [self presentViewController:alter animated:YES completion:nil];
}
- (void)gotoIphoneSet:(NSInteger)index{
    
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if([[UIApplication sharedApplication] canOpenURL:url]) {
        NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
        //此处可以做一下版本适配，至于为何要做版本适配，大家应该很清楚
        [[UIApplication sharedApplication] openURL:url];
    }
}

#pragma mark 请求数据  未分页
- (void)requestMessage{
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"usersId":[BGMUser shareInstance].userId,@"pageNum":@"1",@"pageSize":@"10"};
    [BGMHTTP requestWithMethodName:@"index/getIndexUsersInfo" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if([code isEqualToString:@"200"]){
            [SVProgressHUD dismiss];
            NSArray *array = [result objectForKey:@"data"];
            [self reloadScrollerView:array];
        }else{
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"失败 %@",code]];
        }
    }];
}
- (void)reloadScrollerView:(NSArray *)array{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (![array isKindOfClass:[NSArray class]]) {
            [SVProgressHUD showInfoWithStatus:@"服务器数据返回为空，格式不对，不能解析"];
            return ;
        }
        if(array.count>0){
            [self.userList addObjectsFromArray:array];
            for (NSDictionary *dic in self.userList) {
                NSMutableDictionary *new = [[NSMutableDictionary alloc]init];
                [new setValue:[NSString stringWithFormat:@"%@",[dic objectForKey:@"itisFollow"]] forKey:@"itisFollow"];
                [new setValue:[NSString stringWithFormat:@"%@",[dic objectForKey:@"itisAppent"]] forKey:@"itisAppent"];
                [new setValue:[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]] forKey:@"pid"];
                
                [self.changeData addObject:new];
            }
            
            if (self.userList.count>0) {
                self.homeView.hidden = NO;
                self.whitePage.hidden = YES;
                [self.userCarousel reloadData];
                if(self.currentUserDic == nil){
                    NSDictionary *dic = [self.userList objectAtIndex:0];
                    self.currentUserDic = dic;
                    NSDictionary *newDic = [self.changeData objectAtIndex:0];
                    [self.homeView setMessageWithView:dic changeData:newDic];
                }
               
            }else{
                
            }
        }
        else{
            if (self.userList.count>0) {
                
            }else{
                self.homeView.hidden = YES;
                self.whitePage.hidden = NO;
                [self.whitePage homewhitePage];
                [self.userCarousel reloadData];

            }
        }
    });
}
- (void)requestPublicMessage{
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"usersId":[BGMUser shareInstance].userId,@"pageNum":[NSString stringWithFormat:@"%d",self.currentPublicPage],@"pageSize":[NSString stringWithFormat:@"%d",puclicSize]};
    [BGMHTTP requestWithMethodName:@"index/getIndexG" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if([code isEqualToString:@"200"]){
            [SVProgressHUD dismiss];
            NSArray *array = [result objectForKey:@"data"];
            [self reloadPublicView:array];
        }else{
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"失败 %@",code]];
        }
    }];
}


- (void)reloadPublicView:(NSArray *)array{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.publicList addObjectsFromArray:array];
        if(self.publicList.count==0){
//            self.homeView.hidden = YES;
//            self.whitePage.hidden = NO;
//            [self.whitePage homewhitePage];
//            [self.userCarousel reloadData];
        }else{
            
            
            for (NSDictionary *dic in array) {
                NSMutableDictionary *new = [[NSMutableDictionary alloc]init];
                [new setValue:[NSString stringWithFormat:@"%@",[dic objectForKey:@"itisFollow"]] forKey:@"itisFollow"];
                [new setValue:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pid"]] forKey:@"pid"];
                [self.publicChangeData addObject:new];
            }
            
            [self.publicCarousel reloadData];
            if (array.count == puclicSize) {
                self.publicHaveData = YES;
                self.currentPublicPage = self.currentPublicPage + 1;
            }else{
                self.publicHaveData = NO;
            }
            
            if (self.currentPublicDic == nil) {
                NSDictionary *dic = [self.publicList objectAtIndex:0];
                NSDictionary *otherdic = [self.publicChangeData objectAtIndex:0];
                [self.publicPage setPublicMsg:dic isFollowDic:otherdic];
                self.currentPublicDic = dic;
            }
        }

    });
    
}
#pragma mark 双击请求数据
- (void)freshHomeMsg{
    
    if ([self.currentType isEqualToString:@"0"]) {//用户
        [SVProgressHUD showWithStatus:@"正在请求最新数据"];
        [self.userList removeAllObjects];
        [self.changeData removeAllObjects];
        
        self.currentUserDic = nil;
        [self.userCarousel reloadData];
        [self requestMessage];
    }else{  //公益
        [SVProgressHUD showWithStatus:@"正在请求最新数据"];
        [self.publicList removeAllObjects];
        [self.publicChangeData removeAllObjects];
        
        self.currentPublicPage = 1;
        self.publicHaveData = YES;
        self.currentPublicDic = nil;
        
        self.currentPuclicIndex = 0;
        [self.publicChangeData removeAllObjects];
        
        [self.publicCarousel reloadData];
        [self requestPublicMessage];
    }
    
}

#pragma mark - iCarouselDataSource&&iCarouselDelegate
-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    if (carousel.tag ==100) {
        return self.userList.count>0?self.userList.count:1;
    }else{
        return self.publicList.count>0?self.publicList.count:1;
    }
}
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    
    if (view == nil) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW/4, SCREENW/4)];
        view.layer.cornerRadius =view.frame.size.width/2;
        view.layer.masksToBounds = YES;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width,view.frame.size.height)];
        imageView.layer.cornerRadius =view.frame.size.width/2;
        imageView.layer.masksToBounds = YES;
        imageView.tag = 100;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [view addSubview:imageView];
    }
    UIImageView *imageView = [view viewWithTag:100];
    
    if(carousel.tag ==100){
        if (self.userList.count==0) {
            [imageView setImage:[UIImage imageNamed:@"headImage"]];
        }else{
            NSDictionary *dic = [self.userList objectAtIndex:index];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dic objectForKey:@"userHead"]]] placeholderImage:[UIImage imageNamed:@"headImage"]];
        }
    }else{
        
        if (self.publicList.count==0) {
            [imageView setImage:[UIImage imageNamed:@"headImage"]];
        }else{
            NSDictionary *dic = [self.publicList objectAtIndex:index];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dic objectForKey:@"uheadImage"]]] placeholderImage:[UIImage imageNamed:@"headImage"]];
        }
    }
    return view;
}
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel{
    //     UIView *view = carousel.currentItemView;
    //    view.backgroundColor = [UIColor clearColor];
    //    self.selectView = view;
    
    UIView *currentView = carousel.currentItemView;
    currentView.layer.borderWidth = 5;
    currentView.layer.borderColor = [[UIColor whiteColor]CGColor];
    
    
}
- (void)carouselDidScroll:(iCarousel *)carousel{
    //    if (self.selectView != carousel.currentItemView) {
    //        UIImageView *imageView = [carousel.currentItemView viewWithTag:100];
    //        CGRect rect = imageView.frame;
    //        rect.origin.y = 0;
    //        imageView.frame = rect;
    //
    //    }
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel{
    
    if (carousel.tag == 100) {
        if (self.userList.count ==0) {
            return;
        }else{
            int i = (int)carousel.currentItemIndex;
            self.currentIndex = i;
            NSDictionary *dic = [self.userList objectAtIndex:i];
            self.currentUserDic = dic;
            NSDictionary *otherDic = [self.changeData objectAtIndex:i];
            [self.homeView setMessageWithView:dic changeData:otherDic];
        }
    }else{

        if (self.publicList.count ==0) {
            return;
        }else{
            
            int i = (int)carousel.currentItemIndex;

            NSDictionary *dic = [self.publicList objectAtIndex:i];
            self.currentPuclicIndex = i;
            self.currentPublicDic = dic;
            
            NSDictionary *otherDic = [self.publicChangeData objectAtIndex:i];
            [self.publicPage setPublicMsg:dic isFollowDic:otherDic];
            
            if (self.isSearch) {
               
                if (i == (self.publicList.count-3)) {
                    if (self.publicHaveData) {
                        [self addSearchPublic];
                    }else{
                        NSLog(@"公益无最新数据");
                    }
                }else{
                    
                }
                
                
            }else{
                if (i == (self.publicList.count-3)) {
                    if (self.publicHaveData) {
                        [self requestPublicMessage];
                    }else{
                        NSLog(@"公益无最新数据");
                    }
                }else{
                    
                }
            }
        }
    }
    
    
}
- (void)addSearchPublic{
  
    self.searchPage = self.searchPage+1;
    NSString *searchType = @"";
    
    //9 寻亲团圆 生命接力  爱心助学
    NSDictionary *param = @{@"usersId":[BGMUser shareInstance].userId,
                            @"userToken":[BGMUser shareInstance].userToken,
                            @"publishType":searchType,
                            @"city":@"",
                            @"gender":@"",   //1男2女 空不限
                            @"itisPaid":@"", //1有偿2无偿
                            @"pageNum":[NSString stringWithFormat:@"%d",self.searchPage],
                            @"pageSize":@"10"
                            };
    
    [BGMHTTP requestWithMethodName:@"index/serch" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            NSArray *array = [result objectForKey:@"data"];
            [self reloadPublicView:array];
        }
       
    }];

}


- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform{

    static CGFloat max_sacle = 0.8f;
    static CGFloat min_scale = 0.4f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
//        UIView *currentView = carousel.currentItemView;
//        currentView.layer.borderWidth = 5;
//        currentView.layer.borderColor = [[UIColor whiteColor]CGColor];
        
        
    }else{
//        UIView *currentView = carousel.currentItemView;
//        currentView.layer.borderWidth = 0;
//        currentView.layer.borderColor = [[UIColor whiteColor]CGColor];
        
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    return CATransform3DTranslate(transform, offset * self.userCarousel.itemWidth * 2.5, 0.0, 0.0);
    //间隔
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
